<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="九位码" prop="nineCode">
        <el-input
          v-model="queryParams.nineCode"
          placeholder="请输入九位码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basicInformation:niniDigitCode:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['basicInformation:niniDigitCode:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basicInformation:niniDigitCode:remove']"
        >批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basicInformation:niniDigitCode:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="niniDigitCodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="九位码" align="center" prop="nineCode" />
      <el-table-column label="终端设备名称" align="center" prop="equipmentName" />
      <el-table-column label="终端设备编码" align="center" prop="equipmentNumber" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicInformation:niniDigitCode:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicInformation:niniDigitCode:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改九位码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="九位码" prop="nineCode">
          <el-input v-model="form.nineCode" placeholder="请输入九位码" />
        </el-form-item>
        <el-form-item label="终端设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" readonly />
        </el-form-item>
        <el-form-item label="终端设备编码" prop="equipmentNumber">
          <el-input v-model="form.equipmentNumber" readonly style="width:230px"  />
          <el-button type="primary" v-model="form.terminalId" @click="selectTerminal">选择终端设备</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="titleTerminal" :visible.sync="openTerminal" width="800px" append-to-body>
      <el-form :model="queryTerminalParams" ref="queryTerminalForm" :inline="true" v-show="showSearchTerminal" label-width="68px">
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input
            v-model="queryTerminalParams.equipmentName"
            placeholder="请输入设备名称"
            clearable
            size="small"
            @keyup.enter.native="handleQueryTerminal"
          />
        </el-form-item>
        <el-form-item label="设备编号" prop="equipmentNumber">
          <el-input
            v-model="queryTerminalParams.equipmentNumber"
            placeholder="请输入设备编号"
            clearable
            size="small"
            @keyup.enter.native="handleQueryTerminal"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryTerminal">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetTerminalQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="terminalList" @selection-change="handleSelectionChangeTerminal">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
        <el-table-column label="设备名称" align="center" prop="equipmentName" />
        <el-table-column label="设备编号" align="center" prop="equipmentNumber" />
      </el-table>
      <pagination
        v-show="totalTerminal>0"
        :total="totalTerminal"
        :page.sync="queryTerminalParams.pageNum"
        :limit.sync="queryTerminalParams.pageSize"
        @pagination="getTerminalList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okTerminal">确 定</el-button>
        <el-button @click="cancelTerminal">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNiniDigitCode, getNiniDigitCode, delNiniDigitCode, addNiniDigitCode, updateNiniDigitCode, exportNiniDigitCode } from "@/api/basicInformation/niniDigitCode";
import { listTerminal } from "@/api/basicInformation/terminal";

export default {
  name: "NiniDigitCode",
  components: {
  },
  data() {
    return {
      idsTerminal: [],
      terminalNames: [],
      terminalNumbers: [],
      // 显示搜索条件
      showSearchTerminal: true,
      totalTerminal: 0,
      terminalList: [],
      titleTerminal: "选择终端设备",
      openTerminal: false,
      queryTerminalParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentName: undefined,
        equipmentNumber: undefined,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 九位码表格数据
      niniDigitCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nineCode: undefined,
        orderByColumn: 'createTime',
        isAsc: 'asc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nineCode: [
          { required: true, message: "九位码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getTerminalList() {
      this.loading = true;
      listTerminal(this.queryTerminalParams).then(response => {
        this.terminalList = response.rows;
        this.totalTerminal = response.total;
        this.loading = false;
      });
    },
    okTerminal() {
      if (this.idsTerminal.length > 1) {
        this.msgInfo("只能绑定一个终端设备");
      } else if (this.idsTerminal.length === 1) {
        this.form.terminalId = this.idsTerminal[0];
        this.form.equipmentName = this.terminalNames[0];
        this.form.equipmentNumber = this.terminalNumbers[0];
        this.openTerminal = false;
      } else {
        this.msgInfo("请选择终端设备");
      }
    },
    cancelTerminal() {
      this.openTerminal = false;
    },
    handleQueryTerminal() {
      this.queryTerminalParams.pageNum = 1;
      this.getTerminalList();
    },
    resetTerminalQuery() {
      this.resetForm("queryTerminalForm");
      this.handleQueryTerminal();
    },
    // 多选框选中数据
    handleSelectionChangeTerminal(selection) {
      this.idsTerminal= selection.map(item => item.id)
      this.terminalNames = selection.map(item => item.equipmentName);
      this.terminalNumbers = selection.map(item => item.equipmentNumber);
    },
    selectTerminal() {
      this.openTerminal = true;
      this.handleQueryTerminal();
    },
    /** 查询九位码列表 */
    getList() {
      this.loading = true;
      listNiniDigitCode(this.queryParams).then(response => {
        this.niniDigitCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        nineCode: undefined,
        delFlag: undefined,
        terminalId: undefined,
        createById: undefined,
        createBy: undefined,
        createTime: undefined,
        updateById: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.nineCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加九位码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNiniDigitCode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改九位码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNiniDigitCode(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNiniDigitCode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.nineCode || this.names
      this.$confirm('是否确认删除九位码为"' + names + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNiniDigitCode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有九位码数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportNiniDigitCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
